package Mydictionary;

import java.io.*;
import javax.sound.sampled.Line;

/**
 * 工具实现类文件实现
 */
public class DictionaryDaoIO implements DictionaryDao {
    @Override
    public void save(MyDictionary myDictionary)  {
        //把传入的参数myDictionary对象写入文件D:\\d.txt
        BufferedWriter bw=null;
        try{
bw=new BufferedWriter(new FileWriter("d:\\d.txt",true));
    bw.write(myDictionary.getId()+"--"+myDictionary.getChinese()+
    "--"+myDictionary.getEnglish()+"--"+myDictionary.getComments());
        bw.newLine();
        bw.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    finally {
        if (bw!=null){
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }
    }

    @Override
    public MyDictionary query(String name) {
        /*
       根据传入的英文name，返回字典对象
      循环
         */
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("d:\\d.txt"));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] date = line.split("--");
                if (name.equals(date[1])) {
                    int id = Integer.parseInt(date[0]);
                    String english = date[1];
                    String chinese = date[2];
                    String comments = date[3];
                    MyDictionary d = new MyDictionary(id, english, chinese, comments);
                    return d;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return null;
    }






    @Override
    public void queryAll() {
        BufferedReader br=null;
        try{
            br=new BufferedReader(new FileReader("d:\\d.txt"));
            String line=null;
            while((line=br.readLine())!=null) {
                System.out.println(line);
            }
    } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void delete(String name) {
/*
/判断要删除的内容是否存在
不存在提示不存在
若存在
循环操作：
读取源文件中的每一行文本
比较是不是要删除的内容
若不是写入新文件
最后将原文件删除，新文件重命名
 */
if (query(name)==null){
  System.out.println("要删除的内容不存在");
  return;
}else {
  BufferedWriter bw=null;
  BufferedReader br=null;
  try {


  br=new BufferedReader(new FileReader("d:\\d.txt"));
  String line;
  bw=new BufferedWriter(new FileWriter("d:\\new.txt",true));
while ((line=br.readLine())!=null){
  String[] data=line.split("--");
  if (!name.equals(data[1])){
    bw.write(line);
    bw.newLine();
    bw.flush();
  }
}
  }catch (Exception e){
    e.printStackTrace();
  }finally {
    if(bw!=null){
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (bw!=null){
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
File oldFile=new File("d:\\d.txt");
File newfile=new File("d:\\new.txt");
oldFile.delete();
newfile.renameTo(oldFile);
      System.out.println("删除成公");
    }
}
